package Gui;
import com.cloudgarden.resource.SWTResourceManager;

import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.custom.CCombo;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/

/**
* Description :open an algorithm
* <p>
*
*@author Danielle
*/
public class OpenAlgo extends org.eclipse.swt.widgets.Composite {

	{
		//Register as a resource user - SWTResourceManager will
		//handle the obtaining and disposing of resources
		SWTResourceManager.registerResourceUser(this);
	}
	
	private Composite allView;
	private Button cancel;
	private Text selectFile;
	private Button button1;
	private Button openFolder;
	static Shell shell;
	private Label select;
	static String returnString;
	
	
	/**
	* Overriding checkSubclass allows this class to extend org.eclipse.swt.widgets.Composite
	*/	
	protected void checkSubclass() {
	}
	
	/**
	* Auto-generated method to display this 
	* org.eclipse.swt.widgets.Composite inside a new Shell.
	*/
	public static String showGUI() {
		Display display = Display.getDefault();
		shell = new Shell(display);
		OpenAlgo inst = new OpenAlgo(shell, SWT.NULL);
		Point size = inst.getSize();
		shell.setLayout(new FillLayout());
		shell.layout();
		if(size.x == 0 && size.y == 0) {
			inst.pack();
			shell.pack();
		} else {
			Rectangle shellBounds = shell.computeTrim(0, 0, size.x, size.y);
			shell.setSize(shellBounds.width, shellBounds.height);
		}
		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		return returnString;
	}

	public OpenAlgo(org.eclipse.swt.widgets.Composite parent, int style) {
		super(parent, style);
		initGUI();
	}

	private void initGUI() {
		try {
			FormLayout thisLayout = new FormLayout();
			this.setLayout(thisLayout);
			{
				allView = new Composite(this, SWT.NONE);
				FormLayout allViewLayout = new FormLayout();
				FormData allViewLData = new FormData();
				allViewLData.left =  new FormAttachment(0, 1000, 0);
				allViewLData.top =  new FormAttachment(0, 1000, -6);
				allViewLData.width = 475;
				allViewLData.height = 178;
				allView.setLayoutData(allViewLData);
				allView.setLayout(allViewLayout);
				{
					select = new Label(allView, SWT.NONE);
					FormData selectLData = new FormData();
					selectLData.left =  new FormAttachment(0, 1000, 154);
					selectLData.top =  new FormAttachment(0, 1000, 35);
					selectLData.width = 184;
					selectLData.height = 19;
					select.setLayoutData(selectLData);
					select.setText("Select a new algorithm : ");
					select.setFont(SWTResourceManager.getFont("Tahoma", 11, 0, false, false));
				}
				{
					FormData selectFileLData = new FormData();
					selectFileLData.left =  new FormAttachment(0, 1000, 61);
					selectFileLData.top =  new FormAttachment(0, 1000, 66);
					selectFileLData.width = 308;
					selectFileLData.height = 23;
					selectFile = new Text(allView, SWT.NONE);
					selectFile.setLayoutData(selectFileLData);
				}
				{
					cancel = new Button(allView, SWT.PUSH | SWT.CENTER);
					FormData cancelLData = new FormData();
					cancelLData.left =  new FormAttachment(0, 1000, 301);
					cancelLData.top =  new FormAttachment(0, 1000, 129);
					cancelLData.width = 74;
					cancelLData.height = 23;
					cancel.setLayoutData(cancelLData);
					cancel.setText("cancel");
					cancel.setFont(SWTResourceManager.getFont("Tahoma", 10, 0, false, false));
					cancel.addSelectionListener(new SelectionAdapter() {
						public void widgetSelected(SelectionEvent evt) {
							returnString=null;
							shell.dispose();
							//TODO add your code for cancel.widgetSelected
						}
					});
				}
				{
					button1 = new Button(allView, SWT.PUSH | SWT.CENTER);
					FormData button1LData = new FormData();
					button1LData.left =  new FormAttachment(0, 1000, 92);
					button1LData.top =  new FormAttachment(0, 1000, 129);
					button1LData.width = 74;
					button1LData.height = 23;
					button1.setLayoutData(button1LData);
					button1.setText("open");
					button1.setFont(SWTResourceManager.getFont("Tahoma", 10, 0, false, false));
					button1.addSelectionListener(new SelectionAdapter() {
						public void widgetSelected(SelectionEvent evt) {
							shell.dispose();
							//TODO add your code for button1.widgetSelected
						}
					});
				}
				{
					openFolder = new Button(allView, SWT.PUSH | SWT.CENTER);
					FormData openFolderLData = new FormData();
					openFolderLData.left =  new FormAttachment(0, 1000, 387);
					openFolderLData.top =  new FormAttachment(0, 1000, 66);
					openFolderLData.width = 67;
					openFolderLData.height = 23;
					openFolder.setLayoutData(openFolderLData);
					openFolder.setText("browser");
					openFolder.setFont(SWTResourceManager.getFont("Tahoma", 10, 0, false, false));
					openFolder.addSelectionListener(new SelectionAdapter() {
						public void widgetSelected(SelectionEvent evt) {
								/**
									DirectoryDialog dlg = new DirectoryDialog(parent);
									dlg.setFilterPath(fileName.getText());
									dlg.setText("SWT's DirectoryDialog");
									dlg.setMessage("Select a directory");
							        String dir = dlg.open();
							        if (dir != null) {
							        	fileName.setText(dir);
							        }**/
							        
							        FileDialog fd = new FileDialog(shell, SWT.OPEN);
							        fd.setText("Open");
							        fd.setFilterPath("C:/");
							        String[] filterExt = { "*.txt", "*.doc", ".rtf", "*.*" };
							        fd.setFilterExtensions(filterExt);
							        String selected = fd.open();
							        //System.out.println(selected);
							        
							        if (selected != null) {
							        	selectFile.setText(selected);
							        	returnString=selected;
							        
							        }
						}
						
					});
				}
			}
			this.layout();
			pack();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
